<template>
  <el-dialog title="详情" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="财务分类" prop="parentName">
        <el-popover
          ref="dataListPopover"
          placement="bottom-start"
          trigger="click" disabled>
          <el-tree
            :data="dataList"
            :props="dataListTreeProps"
            node-key="id"
            ref="dataListTree"
            @current-change="dataListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:dataListPopover :readonly="true" placeholder="点击选择分类" class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item label="订单编号" prop="orderId">
        <el-select v-model="dataForm.orderId" filterable placeholder="订单编号" @change="getOrderStudent" disabled>
            <el-option
              v-for="item in orderList"
              :key="item.id"
              :label="item.id"
              :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="学员姓名" prop="studentName">
         <el-input v-model="dataForm.studentName" placeholder="学员姓名" readonly></el-input>
      </el-form-item>
      <el-form-item label="退费状态" prop="refundStatus">
        <el-select v-model="dataForm.refundStatus" placeholder="退费状态" id="status" disabled>
          <el-option v-for="(item, index) in options" :label="item.label" :key="item.value" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="已付金额" prop="amountPaid">
        <el-input v-model="dataForm.amountPaid" placeholder="已付金额" readonly></el-input>
      </el-form-item>
      <el-form-item label="应扣金额" prop="deductibleAmount">
        <el-input v-model="dataForm.deductibleAmount" placeholder="应扣金额" readonly></el-input>
      </el-form-item>
      <el-form-item label="应退金额" prop="refundableAmount">
        <el-input v-model="dataForm.refundableAmount" placeholder="应退金额" readonly></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="dataForm.remarks" placeholder="备注" readonly></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          category: '',
          pid: '',
          fatherCategory: [],
          parentId: 0,
          parentName: '',
          orderId: null,
          studentName: '',
          studentId: null,
          refundStatus: '',
          amountPaid: '',
          deductibleAmount: '',
          refundableAmount: '',
          amountPayable: '',
          remarks: ''
        },
        options: [{
          value: '0',
          label: '退费审核中'
        }, {
          value: '1',
          label: '已退费'
        }, {
          value: '2',
          label: '已撤销'
        }, {
          value: '3',
          label: '已驳回'
        }],
        dataRule: {
          orderId: [{
            required: true,
            message: '订单编号不能为空',
            trigger: 'blur'
          }],
          refundStatus: [{
            required: true,
            message: '退费状态不能为空',
            trigger: 'blur'
          }],
          amountPaid: [{
            required: true,
            message: '已付金额不能为空',
            trigger: 'blur'
          }],
          deductibleAmount: [{
            required: true,
            message: '应扣金额不能为空',
            trigger: 'blur'
          }],
          refundableAmount: [{
            required: true,
            message: '应退金额不能为空',
            trigger: 'blur'
          }]
        },
        dataList: [],
        studentList: [],
        orderList: [],
        dataListTreeProps: {
          label: 'category',
          children: 'children'
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/cwRefund/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm.parentId = data.cwRefund.categoryId
                this.dataForm.parentName = data.cwRefund.categoryName
                this.dataForm.orderId = data.cwRefund.orderId
                this.dataForm.studentName = data.cwRefund.studentName
                this.dataForm.studentId = data.cwRefund.studentId
                // this.dataForm.refundStatus = data.cwRefund.refundStatus
                if (data.cwRefund.refundStatus === 0) {
                  this.dataForm.refundStatus = '退费审核中'
                } else if (data.cwRefund.refundStatus === 1) {
                  this.dataForm.refundStatus = '已退费'
                } else if (data.cwRefund.refundStatus === 2) {
                  this.dataForm.refundStatus = '已撤销'
                } else if (data.cwRefund.refundStatus === 3) {
                  this.dataForm.refundStatus = '已驳回'
                }
                this.dataForm.amountPaid = data.cwRefund.amountPaid
                this.dataForm.deductibleAmount = data.cwRefund.deductibleAmount
                this.dataForm.refundableAmount = data.cwRefund.refundableAmount
                this.dataForm.remarks = data.cwRefund.remarks
              }
            })
          }
        })
      },
      // 查询一级分类
      getFatherCategory () {
        this.$http({
          url: this.$http.adornUrl('/business/cwCategory/list'),
          method: 'get',
          params: this.$http.adornParams({

          })
        }).then(({
          data
        }) => {
          this.dataList = treeDataTranslate(data, 'id', 'pid')
        })
      },
      // 获取学员
      getOrderStudent (orderId) {
        this.$http({
          url: this.$http.adornUrl('/business/student/getOrderStudent'),
          mothod: 'get',
          params: {'orderId': orderId}
        }).then(({data}) => {
          this.dataForm.studentId = data.list.id
          this.dataForm.studentName = data.list.studentName
          console.log(this.dataForm.studentName)
        })
      },
      // 获取订单
      getOrderList () {
        this.$http({
          url: this.$http.adornUrl('/business/cwOrder/getOrderList'),
          mothod: 'get',
          params: {}
        }).then(({data}) => {
          this.orderList = data.list
        })
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/cwRefund/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'categoryId': this.dataForm.parentId,
                'orderId': this.dataForm.orderId,
                'studentName': this.dataForm.studentName,
                'studentId': this.dataForm.studentId,
                'refundStatus': this.dataForm.refundStatus,
                'amountPaid': this.dataForm.amountPaid,
                'deductibleAmount': this.dataForm.deductibleAmount,
                'refundableAmount': this.dataForm.refundableAmount,
                'remarks': this.dataForm.remarks
              })
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 菜单树选中
      dataListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parentId = data.id
        this.dataForm.parentName = data.category
        this.pClose()
      },
      pClose () {
        this.$refs[`dataListPopover`].doClose()
      }
    }
  }
</script>
